package com.test.jwt;

import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTHeader;
import cn.hutool.jwt.JWTUtil;
import cn.hutool.jwt.JWTValidator;
import cn.hutool.jwt.signers.JWTSigner;
import cn.hutool.jwt.signers.JWTSignerUtil;

import com.google.common.base.Strings;
import com.test.utils.AESUtils;
import com.test.utils.DateUtil;
import com.test.utils.RSAHelper;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;

public class JwtHTDemo {

    private static String PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCB7z5AwVZPasnsK6In4jA+IiN68+5yVYu7HTm7Rhs2tGOJQraiQrhJcyF6boUSopaSTv+hhUbELMI0EkCwoqKJ/68YmKoKy7mVDAHBKvNuvskfBU8ljVdAcUFQsiwc8QW4B/ZCu3MCr1yAVKHzEs25jRBhKDaKq60JwId13jAjxGEJMDwn7mJAk/6ZIhmIxrNnsjwEwOMngwpDGcOnirrQTqCAtSY47UylthfULThcCyDXXGYiA6MuQPe5c4h1vp1tydNvOzozqEuoNzPjTta88nrzIfn3T3OvRno/l13ge3v5E7ewLhxg8a5ESN6IWGkhD+zchM1798Z/FzzxZ5QRAgMBAAECggEATQRARCXVQRGtSn26iiV6LvoRScnY5pBqWKjEFYtCijR2Y1/V3WvqzC6gA3ALFLamxWZqA5V6EsHtAuxmx8w+x8l0QgILmIaVJBGP4K3SySvxDqAY/8s9kocPDgPuPeFnYGPnxjpRu5+yFqcTr37PzlIdCVnKqnuD/6nNIUJoCmp+T/jjPo3F5eZPPEOuZQ69vJ8eQYEQISymr/t5/gq6exT7uBi8vQ19xxlLBEZDUI5EioCuRhrUs9zuZZhSeFMGn2Ey+VHnBcMMs3aKKRy2B1arJjg45gCNvZqXoMreXiDENorqlSe1LwVfKOrblQvQ2xlB2h4f/eC65D5KGAsmuQK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ULIsHPEFuAf2QrtzAq9cgFSh8xLNuY0QYSg2iqutCcCHdd4wI8RhCTA8J+5iQJP+mSIZiMazZ7I8BMDjJ4MKQxnDp4q60E6ggLUmOO1MpbYX1C04XAsg11xmIgOjLkD3uXOIdb6dbcnTbzs6M6hLqDcz407WvPJ68yH5909zr0Z6P5dd4Ht7+RO3sC4cYPGuREjeiFhpIQ/s3ITNe/fGfxc88WeUEQIDAQAB";

    public static void main(String[] args) throws Exception {
        demo1();
        demo2();
        demo3();
        System.out.println(Strings.padStart("453441", 5, '0'));
    }

    public static void demo1() throws Exception {
        JWTSigner signer = JWTSignerUtil.rs256(RSAHelper.base64ToPrivateKey(PRIVATE_KEY));
        Map<String, Object> map = new HashMap<String, Object>();
        String data = "NexusToArtifactory 该工具旨在简化从 Sonatype Nexus 到 JFrog Artifactory 的过渡。 它提供了一个简单的类似向导的界面，允许您轻松配置和运行从 Nexus 实例到 Artifactory 实例的存储库、工件、用户和设置的迁移。";
        map.put("uid", 123);
        map.put("expire_time", System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 15);
        map.put("data", AESUtils.encode(data, "123456"));
        for (int i = 0; i < 100; i++) {
            map.put("data" + i, i);
        }
        String str = JWT.create()
                .setIssuer("sunz")
                .setIssuedAt(DateUtil.nowDate())
                .setExpiresAt(DateUtil.ldt2Date(DateUtil.ldtPlus(DateUtil.nowLdt(), ChronoUnit.SECONDS, 1)))
//                .setNotBefore(DateUtil.ldt2Date(DateUtil.ldtPlus(DateUtil.nowLdt(), ChronoUnit.MINUTES, 1)))
                .addPayloads(map)
                .sign(signer)
        ;
        System.out.println(Strings.lenientFormat("String jsonWebToken = \"%s\";", str));
    }

    public static void demo2() {
        String jsonWebToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJzdW56IiwiaWF0IjoxNjU1MTgzMDA0LCJleHAiOjE2NTUxODMwMDUsInVpZCI6MTIzLCJleHBpcmVfdGltZSI6MTY1NjQ3OTAwMzkyM30.dc42D26gEsYdQekqswVjJJa1-GNs4wCsKtmdvh5ReVZHWqbJRC6BBPnNsrXizkki4Gj_CVczM_SZoUbhg4LR7QQ_TwGS6W3IwkDb5k90Nu9IvKOlQzubQU2LQzE6cG5PWNAWQrfiEy8meQ3JKsgfO5DMNYQ9syw0uZWPRmUAO6_6vzoTo35jziegXjd_LASLVmNaLoBylgvD1nucs6itvf9kW337EzPci3i7_ugIejssOUn0BWoGT2aNCctZyir8sip9duV6gtlvCl5IDAiTdeFM44I6KQXO-ogzpDJJ6JzzQAhVv3syOiMP4an7cnbxsj0R9XD0031d4uLFAA0pSg";

        final JWT jwt = JWTUtil.parseToken(jsonWebToken);
        System.out.println(jwt.getHeader(JWTHeader.TYPE));
        System.out.println(jwt.getHeader(JWTHeader.ALGORITHM));

        System.out.println(jwt.getPayload("uid"));
        System.out.println(jwt.getPayload("expire_time"));
        try {
            JWTValidator.of(jsonWebToken).validateDate();
            System.out.println("ok");
        } catch (ValidateException ve) {
            System.out.println("no ok");
        }
    }

    public static void demo3() throws NoSuchAlgorithmException, InvalidKeySpecException {
        String jsonWebToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJzdW56IiwiaWF0IjoxNjU1MTgzMDA0LCJleHAiOjE2NTUxODMwMDUsInVpZCI6MTIzLCJleHBpcmVfdGltZSI6MTY1NjQ3OTAwMzkyM30.dc42D26gEsYdQekqswVjJJa1-GNs4wCsKtmdvh5ReVZHWqbJRC6BBPnNsrXizkki4Gj_CVczM_SZoUbhg4LR7QQ_TwGS6W3IwkDb5k90Nu9IvKOlQzubQU2LQzE6cG5PWNAWQrfiEy8meQ3JKsgfO5DMNYQ9syw0uZWPRmUAO6_6vzoTo35jziegXjd_LASLVmNaLoBylgvD1nucs6itvf9kW337EzPci3i7_ugIejssOUn0BWoGT2aNCctZyir8sip9duV6gtlvCl5IDAiTdeFM44I6KQXO-ogzpDJJ6JzzQAhVv3syOiMP4an7cnbxsj0R9XD0031d4uLFAA0pSg";

        JWTSigner signer = JWTSignerUtil.rs256(RSAHelper.base64ToPublicKey(PUBLIC_KEY));
        System.out.println(JWTUtil.verify(jsonWebToken, signer));
    }
}
